#include <iostream>
using namespace std;

int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  int T;
  for (cin >> T; T; T--) {
    int n;
    long long ans = 0, m3 = 1;

    cin >> n;
    for (int x = 0; n > 0; x++, m3 *= 3, n /= 3) {
      ans += (n % 3) * (m3 * 3 + x * m3 / 3);
    }
    cout << ans << "\n";
  }
  return 0;
}